package com.ruoyi.system.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.domain.entity.YhOrder;
import com.ruoyi.common.enums.PayStatusEnum;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.enums.CashierEnum;
import com.ruoyi.system.kit.dto.PayParam;
import com.ruoyi.system.kit.dto.PaymentSuccessParams;
import com.ruoyi.system.kit.params.CashierExecute;
import com.ruoyi.system.kit.params.dto.CashierParam;
import com.ruoyi.system.service.IYhOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 订单支付信息获取
 *
 * @author Chopper
 * @since 2021-01-25 20:00
 */
@Slf4j
@Component
public class OrderCashier implements CashierExecute {
    /**
     * 订单
     */
    @Autowired
    private IYhOrderService orderService;
//    /**
//     * 设置
//     */
//    @Autowired
//    private SettingService settingService;

    @Override
    public CashierEnum cashierEnum() {
        return CashierEnum.ORDER;
    }

    @Override
    public CashierParam getPaymentParams(PayParam payParam) {
        if (payParam.getOrderType().equals(CashierEnum.ORDER.name())) {
            //准备返回的数据
            CashierParam cashierParam = new CashierParam();
            //订单信息获取
            YhOrder yhOrder = orderService.getOne(new LambdaQueryWrapper<YhOrder>().eq(YhOrder::getSn, payParam.getSn()));

            //如果订单已支付，则不能发器支付
            if (yhOrder.getOrderStatus().equals(PayStatusEnum.PAID.name())) {
                throw new ServiceException("订单已支付！");
            }
            //如果订单状态不是待付款，则抛出异常
            if (!yhOrder.getOrderStatus().equals(PayStatusEnum.UNPAID.name())) {
                throw new ServiceException("订单状态异常！");
            }
            cashierParam.setPrice(yhOrder.getFlowPrice());
//            cashierParam.setPrice(0.01D);

            try {
//                BaseSetting baseSetting = JSONUtil.toBean(settingService
//                        .get(SettingEnum.BASE_SETTING.name()).getSettingValue(), BaseSetting.class);
//                cashierParam.setTitle(baseSetting.getSiteName());
                cashierParam.setTitle("购买" + yhOrder.getMemberLevel() + "级会员");
            } catch (Exception e) {
                cashierParam.setTitle("多用户商城，在线支付");
            }
//            List<OrderItem> orderItemList = order.getOrderItems();
//            StringBuilder subject = new StringBuilder();
//            for (OrderItem orderItem : orderItemList) {
//                subject.append(orderItem.getGoodsName()).append(";");
//            }

//            cashierParam.setDetail(subject.toString());
//            Setting setting = settingService.get(SettingEnum.PAY_NAME.name());
//            PayNameSetting ossSetting =  JSONUtil.toBean(setting.getSettingValue(), PayNameSetting.class);
//            cashierParam.setDetail(ossSetting.getName());
            cashierParam.setDetail("购买" + yhOrder.getMemberLevel() + "级会员");

            cashierParam.setOrderSns(payParam.getSn());
//            cashierParam.setCreateTime(order.getOrder().getCreateTime());
            cashierParam.setCreateTime(new Date());
            return cashierParam;
        }

        return null;
    }

    @Override
    public void paymentSuccess(PaymentSuccessParams paymentSuccessParams) {

        PayParam payParam = paymentSuccessParams.getPayParam();
        if (payParam.getOrderType().equals(CashierEnum.ORDER.name())) {
            // TODO 支付成功逻辑处理
            orderService.payOrder(payParam.getSn(),
                    paymentSuccessParams.getPaymentMethod(),
                    paymentSuccessParams.getReceivableNo());
            log.info("订单{}支付成功,金额{},方式{}", payParam.getSn(),
                    paymentSuccessParams.getPaymentMethod(),
                    paymentSuccessParams.getReceivableNo());
        }
    }

    @Override
    public Boolean paymentResult(PayParam payParam) {
        if (payParam.getOrderType().equals(CashierEnum.ORDER.name())) {

            if (payParam != null) {
                YhOrder yhOrder = orderService.getOne(new LambdaQueryWrapper<YhOrder>()
                        .eq(YhOrder::getSn, payParam.getSn()));
                if(yhOrder != null) {
                    return PayStatusEnum.PAID.name().equals(yhOrder.getOrderStatus());
                } else {
                    throw new ServiceException("支付订单不存在");
                }
            } else {
                throw new ServiceException("支付订单不存在");
            }
        }
        return false;
    }
}
